﻿

#include "NpcManager.h"
#include "MapManager.h"
#include "BattleManager.h"

NpcManager* gNpcManager = F_NULL;
MapCommand2	NpcManager::mCommands;

extern RECV_MSG_TALK_NPC gMsgTalkNpc;
extern RECV_MSG_BATTLE gRecvMsgBattle;

NpcManager::NpcManager()
{
	gNpcManager = this;
    mNpcVector.reserve( MAX_NPC );
    mARGDataVector.reserve( MAX_NPC );
    mCreateDataVector.reserve( MAX_NPC );
    mTemplateDataVector.reserve( MAX_NPC );
    
}


NpcManager::~NpcManager()
{
	
}


fvoid		NpcManager::Init()
{
	mCommands[ SANT_WARPMAN ] = CommandWarpMan;
	mCommands[ SANT_WARP ] = CommandWarp;
	mCommands[ SANT_SAVEPOINT ] = CommandSavePoint;
	mCommands[ SANT_FIGHT ] = CommandFight;
	mCommands[ SANT_QUEST ] = CommandQuest;
	mCommands[ SANT_SHOP ] = CommandShop;
	mCommands[ SANT_MAN ] = CommandMan;
	mCommands[ SANT_HEALER ] = CommandHealer;
	mCommands[ SANT_CHARM ] = CommandCharm;
	mCommands[ SANT_PETSHOP ] = CommandPetShot;
	mCommands[ SANT_SIGNBOARD ] = CommandSignBoard;
	mCommands[ SANT_DENGON ] = CommandDengon;
	mCommands[ SANT_POOLITEMSHOP ] = CommandPoolItemShop;
	mCommands[ SANT_TRANSMAN ] = CommandTransMan;

	mCommands[ SANT_ITEMCHANGE ] = CommandItemChange;
	mCommands[ SANT_PROFESSIONSHOP ] = CommandProfessionShop;
	mCommands[ SANT_FREEPETSKILL ] = CommandFreePetSkill;
	mCommands[ SANT_PETSKILLSHOP ] = CommandPetSkillShop;
	mCommands[ SANT_PETFUSION ] = CommandPetFusion;
	mCommands[ SANT_WINDOWMAN ] = CommandWindowMan;
	mCommands[ SANT_LIMITSHOP ] = CommandLimitShop;
	mCommands[ SANT_WINDOWMAN ] = CommandWarpMan;
	mCommands[ SANT_TIMEMAN ] = CommandTimeMan;
	mCommands[ SANT_LUCKYMAN ] = CommandLuckyMan;
	mCommands[ SANT_FAMILYMAN ] = CommandFamilyMan;
	mCommands[ SANT_BANKMAN ] = CommandBankMan;
	mCommands[ SANT_GAMBLEBANK ] = CommandGambleBank;


}


GameNpc*    NpcManager::GetNpc( fint32 id )
{
    GameNpcMapIter iter = mNpcMap.find( id );
    
    if ( iter != mNpcMap.end() )
    {
        return iter->second;
    }
    
    return F_NULL;
}

GameNpc*    NpcManager::CreateNpc( NpcCreateData* data )
{
    // create and don't destory,,
    
    GameNpc* npc = new GameNpc();
    fint32 npcID = (fint32)mNpcVector.size() + 1;
    npc->SetGUID( npcID );
    
    mNpcVector.push_back( npc );
    
    mNpcMap[ npcID ] = npc;
    
    if ( mNpcVector.size() == MAX_NPC )
    {
        assert( 0 );
    }
    
    return npc;
}

fvoid		NpcManager::Release()
{
    
}

fvoid       NpcManager::Load( const fbyte* path )
{
    fileVector vector;
    fileTools::getSubFiles( path , vector );
    
    for ( fint32 i = 0 ; i < vector.size() ; ++i )
    {
        string& name = vector[ i ];
        
        if ( fileTools::getExtensionName( name.c_str() , "create" ) )
        {
            NpcCreateData* createData = new NpcCreateData();
            
            ptree pt;
            ptree root;
            
            read_xml( path , pt , boost::property_tree::xml_parser::trim_whitespace );
            
            FLOG4( "xml loading. %s" , path );
            
            root = pt.get_child( "root" );
            
            for ( ptree::assoc_iterator iter = root.ordered_begin(); iter != root.not_found() ; ++iter )
            {
                ptree n = iter->second;
                
                NpcTemplateData* templateData = new NpcTemplateData();
                
                templateData->Template = n.get< string >( "<xmlattr>.template" );
                templateData->Name = n.get< string >( "<xmlattr>.name" );
                templateData->Fun = n.get< string >( "<xmlattr>.fun" );
                templateData->ImgName = n.get< string >( "<xmlattr>.imgName" );
                templateData->NoBody = n.get< fint32 >( "<xmlattr>.nobody" );
                templateData->NoSee = n.get< fint32 >( "<xmlattr>.nosee" );
                templateData->Type = n.get< fint32 >( "<xmlattr>.type" , 0 );
                templateData->HP = n.get< fint32 >( "<xmlattr>.hp" , 0 );
                templateData->MP = n.get< fint32 >( "<xmlattr>.mp" , 0 );
                templateData->Str = n.get< fint32 >( "<xmlattr>.str" , 0 );
                templateData->Tough = n.get< fint32 >( "<xmlattr>.tough" , 0 );
                templateData->LoopTime = n.get< fint32 >( "<xmlattr>.loopTime" , 0 );
                templateData->Fly = n.get< fint32 >( "<xmlattr>.fly" , 0 );
                templateData->ItemNum = n.get< fint32 >( "<xmlattr>.itemNum" , 0 );
                
                mTemplateDataMap[ templateData->Template ] = templateData;
                mTemplateDataVector.push_back( templateData );
            }
            
            FLOG4( "xml loaded. %s" , path );
        }
    }
}

fvoid       NpcManager::LoadTemplate( const fbyte* path )
{
    ptree pt;
    ptree root;
    
    read_xml( path , pt , boost::property_tree::xml_parser::trim_whitespace );
    
    FLOG4( "xml loading. %s" , path );
    
    root = pt.get_child( "root" );
    
    for ( ptree::assoc_iterator iter = root.ordered_begin(); iter != root.not_found() ; ++iter )
    {
        ptree n = iter->second;
        
        NpcTemplateData* templateData = new NpcTemplateData();
        
        templateData->Template = n.get< string >( "<xmlattr>.template" );
        templateData->Name = n.get< string >( "<xmlattr>.name" );
        templateData->Fun = n.get< string >( "<xmlattr>.fun" );
        templateData->ImgName = n.get< string >( "<xmlattr>.imgName" );
        templateData->NoBody = n.get< fint32 >( "<xmlattr>.nobody" );
        templateData->NoSee = n.get< fint32 >( "<xmlattr>.nosee" );
        templateData->Type = n.get< fint32 >( "<xmlattr>.type" , 0 );
        templateData->HP = n.get< fint32 >( "<xmlattr>.hp" , 0 );
        templateData->MP = n.get< fint32 >( "<xmlattr>.mp" , 0 );
        templateData->Str = n.get< fint32 >( "<xmlattr>.str" , 0 );
        templateData->Tough = n.get< fint32 >( "<xmlattr>.tough" , 0 );
        templateData->LoopTime = n.get< fint32 >( "<xmlattr>.loopTime" , 0 );
        templateData->Fly = n.get< fint32 >( "<xmlattr>.fly" , 0 );
        templateData->ItemNum = n.get< fint32 >( "<xmlattr>.itemNum" , 0 );
        
        mTemplateDataMap[ templateData->Template ] = templateData;
        mTemplateDataVector.push_back( templateData );
    }
    
    FLOG4( "xml loaded. %s" , path );
}


fvoid		NpcManager::TalkNpc( SANpcType type , GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
	mCommands[ type ]( n , p , msg );
}

fvoid		NpcManager::CommandWarpMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
//	NpcCreateData& data = n->GetNpcCreateData();
//	
//	if ( msg->Select == INVALID_ID && msg->State == INVALID_ID )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_MODE1;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.NormalMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//
//		fint32 count = data.Select.size();
//		for ( fint32 i = 0 ; i < count ; i++ )
//		{
//			MEMCPY( gMsgTalkNpc.Data + size , data.Select[i].c_str() , data.Select[i].size() );
//			size += data.Select[i].size();
//			gMsgTalkNpc.Data[ size ] = '\n';size++;
//		}
//
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//		p->SendMsg();
//	}
//
//	if ( msg->Select != INVALID_ID && data.SelectData.size() > msg->Select )
//	{
//		MapPos pos;
//		pos.Grid.x = data.SelectData[ msg->Select ].WarpX;
//		pos.Grid.y = data.SelectData[ msg->Select ].WarpY;
//		pos.MapID = data.SelectData[ msg->Select ].WarpID;
//
//		gMapManager->WarpScene( p , pos );
//	}
}

fvoid		NpcManager::CommandWarp( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandSavePoint( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
	
}
fvoid		NpcManager::CommandFight( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
//	NpcCreateData& data = n->GetNpcCreateData();
//
//	if ( msg->Select == INVALID_ID && msg->State == INVALID_ID )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_MODE2;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.NormalMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//		p->SendMsg();
//
//		return;
//	}
//
//	if ( msg->State == SANS_ALREADY )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_NULL;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.AlreadyMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//
//		gBattleManager->CreatBattle( &data , p );
//
//		p->PackMsg( &gRecvMsgBattle );
//		p->SendMsg();
//		p->StartFight();
//
//		GameTeam* team = p->GetTeam();
//
//		if ( team )
//		{
//			fint32 count = team->GetMemberCount();
//
//			for ( fint32 i = 1 ; i < count ; i++ )
//			{
//				team->GetMember( i )->PackMsg( &gMsgTalkNpc );
//				team->GetMember( i )->PackMsg( &gRecvMsgBattle );
//				team->GetMember( i )->SendMsg();
//				team->GetMember( i )->StartFight();
//			}
//		}
//
//		return;
//	}
//
//	if ( msg->State == SANS_DENIED )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_NULL;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.DeniedMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//		
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//		p->SendMsg();
//
//		return;
//	}
}
fvoid		NpcManager::CommandQuest( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandShop( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandHealer( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
//	NpcCreateData& data = n->GetNpcCreateData();
//
//	if ( msg->Select == INVALID_ID && msg->State == INVALID_ID )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_MODE1;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.NormalMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//
//		fint32 count = data.Select.size();
//		for ( fint32 i = 0 ; i < count ; i++ )
//		{
//			MEMCPY( gMsgTalkNpc.Data + size , data.Select[i].c_str() , data.Select[i].size() );
//			size += data.Select[i].size();
//			gMsgTalkNpc.Data[ size ] = '\n';size++;
//		}
//
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//		p->SendMsg();
//	}
//
//	if ( msg->Select != INVALID_ID )
//	{
//		p->Heal( msg->Select );
//	}
}
fvoid		NpcManager::CommandCharm( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{
//	NpcCreateData& data = n->GetNpcCreateData();
//
//	if ( msg->Select == INVALID_ID && msg->State == INVALID_ID )
//	{
//		gMsgTalkNpc.ID = data.ID;
//		gMsgTalkNpc.Type = data.Type;
//		gMsgTalkNpc.Mode = SANM_MODE1;
//
//		fint32 size = data.NormalMsg.size();
//		MEMCPY( gMsgTalkNpc.Data , data.NormalMsg.c_str() , size );
//		gMsgTalkNpc.Data[ size ] = '\n';size++;
//
//		fint32 count = data.Select.size();
//		for ( fint32 i = 0 ; i < count ; i++ )
//		{
//			MEMCPY( gMsgTalkNpc.Data + size , data.Select[i].c_str() , data.Select[i].size() );
//			size += data.Select[i].size();
//			gMsgTalkNpc.Data[ size ] = '\n';size++;
//		}
//
//		gMsgTalkNpc.size = 12 + size;
//		p->PackMsg( &gMsgTalkNpc );
//		p->SendMsg();
//	}
//
//	if ( msg->Select != INVALID_ID )
//	{
//		
//	}
}
fvoid		NpcManager::CommandPetShot( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandSignBoard( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandDengon( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandPoolItemShop( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandTransMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandItemChange( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandProfessionShop( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandFreePetSkill( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandPetSkillShop( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandPetFusion( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandWindowMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandLimitShop( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandTimeMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandLuckyMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandFamilyMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandBankMan( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
fvoid		NpcManager::CommandGambleBank( GameNpc* n , GamePlayer* p , SEND_MSG_TALK_NPC* msg )
{

}
